package com.spdg.ring.adapter;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wolf.tools.StringUtil;

import com.spdg.ring.R;
import com.spdg.ring.sqlite.model.ArticleList;
import com.spdg.ring.util.ImageLoader;

/**
 * <p>Title: 纯列表适配器   </p>
 * <p>Description: 
 * 仅包含列表的适配器
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-28             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ListAdapter extends BaseAdapter {

	private List<ArticleList> mContentList = null;
	private Context mContext;
	private final LayoutInflater mInflater;

	public ListAdapter(Context ctx, List<ArticleList> list) {
		this.mContext = ctx;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (list == null) {
			mContentList = Collections.emptyList();
		}

		this.mContentList = list;
	}

	@Override
	public int getCount() {
		return mContentList.size();
	}

	@Override
	public Object getItem(int pos) {
		return mContentList.get(pos);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	// FIXME 图片下载，需要修改
	@Override
	public View getView(int pos, View v, ViewGroup parent) {
		if (v == null) {
			v = mInflater.inflate(R.layout.listview_item, null);
		}

		ImageView mImgIcon = (ImageView) v.findViewById(R.id.list_icon);
		TextView mTvTitle = (TextView) v.findViewById(R.id.list_title);
		TextView mTvDetail = (TextView) v.findViewById(R.id.list_detail);
		TextView mTvGroupTitle = (TextView) v.findViewById(R.id.list_group_title);

		ArticleList entity = mContentList.get(pos);
		String group = entity.getGroup();
		if (pos > 0) {
			ArticleList lastEntity = mContentList.get(pos - 1);
			String lastGroup = lastEntity.getGroup();

			if (!StringUtil.isEmpty(group) && !group.equals(lastGroup)) {
				mTvGroupTitle.setText(group);
				mTvGroupTitle.setVisibility(View.VISIBLE);
			} else {
				mTvGroupTitle.setVisibility(View.GONE);
			}
		} else {
			if (!StringUtil.isEmpty(group)) {
				mTvGroupTitle.setText(group);
				mTvGroupTitle.setVisibility(View.VISIBLE);
			} else {
				mTvGroupTitle.setVisibility(View.GONE);
			}
		}

		String imgUrl = entity.getIcon();
		mImgIcon.setImageResource(R.drawable.default_image);
		ImageLoader.getInstance().loadBitmapOvri(mContext, mImgIcon, imgUrl, entity.getIconFile());

		String title = entity.getName();
		mTvTitle.setText(title);

		String summary = entity.getSummary();
		if (StringUtil.isEmpty(summary)) {
			mTvDetail.setVisibility(View.GONE);
		} else {
			mTvDetail.setText(Html.fromHtml(summary));
			mTvDetail.setVisibility(View.VISIBLE);
		}

		return v;
	}

}
